//
//  RestaurantDAO.m
//  restaurant
//
//  Created by dongpoyezi on 5/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RestaurantDAO.h"
#import "FileHelper.h"
#import "Restaurant.h"

@implementation RestaurantDAO

+(NSMutableArray*)getRestaurantByArea:(NSString *)area currentIndex:(NSUInteger)index{
	sqlite3 *database;
	
	NSMutableArray *restaurantList =[[NSMutableArray alloc] init];
	
	if (sqlite3_open([[FileHelper dataFilePath] UTF8String], &database)==SQLITE_OK) {
		//NSString *query =@"SELECT NAME,ADDR,TEL,LABEL from FOODINFO  where (name like '%"+双林路+"%') or (addr like '%"+双林路+"%') or(busi_region like  '%"+双林路+"%') LIMIT 0,10";
		//NSString *query =@"SELECT NAME,ADDR,TEL,LABEL from FOODINFO  where (name like '%";
//		query=[query stringByAppendingString:key];
//		query=[query stringByAppendingString:@"%') or (addr like '%"];
//		query=[query stringByAppendingString:key];
//		query=[query stringByAppendingString:@"%') or(busi_region like  '%"];
//		query=[query stringByAppendingString:key];
//		query=[query stringByAppendingString:@"%') LIMIT 0,10"];
		
		NSString *query =@"SELECT ID,NAME,ADDR,TEL,LABEL from FOODINFO  where busi_region like '%";
		query=[query stringByAppendingString:area];
		query=[query stringByAppendingString:@"%'"];
		query =[query stringByAppendingFormat:@" LIMIT %d,10",index];
		//query=[query stringByAppendingString:@""];
		
		NSLog(@"query --%@",query);
		sqlite3_stmt *statement;
		if (sqlite3_prepare_v2(database,[query UTF8String], -1, &statement, nil)== SQLITE_OK) {
			while (sqlite3_step(statement) ==SQLITE_ROW) {
				NSUInteger rid =sqlite3_column_int(statement, 0);
				NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
				NSString *addr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
				NSString *tel = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
				NSString *label = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,4)];
				
				Restaurant *restaurant =[[Restaurant alloc]init];
				restaurant.rid=rid;
				restaurant.name =name;
				restaurant.addr =addr;
				restaurant.tel =tel;
				restaurant.label=label;
				[restaurantList addObject:restaurant];
				
				[restaurant release];
				//NSLog(@"id --%d",row);
				
			}
			sqlite3_finalize(statement);
		}
		
	}
	sqlite3_close(database);
	return restaurantList;

}

+(NSMutableArray*)getRestaurantByKey:(NSString *)key currentIndex:(NSUInteger)index{
    sqlite3 *database;
	
	NSMutableArray *restaurantList =[[NSMutableArray alloc] init];
	
	if (sqlite3_open([[FileHelper dataFilePath] UTF8String], &database)==SQLITE_OK) {
		     NSString *query =@"SELECT ID, NAME,ADDR,TEL,LABEL from FOODINFO  where (name like '%";
			 query=[query stringByAppendingString:key];
			 query=[query stringByAppendingString:@"%') or (addr like '%"];
			 query=[query stringByAppendingString:key];
			 query=[query stringByAppendingString:@"%') or(busi_region like  '%"];
			 query=[query stringByAppendingString:key];
		     query=[query stringByAppendingString:@"%')"];
			 query =[query stringByAppendingFormat:@" LIMIT %d,10",index];
		     
			NSLog(@"query --%@",query);
			sqlite3_stmt *statement;
			if (sqlite3_prepare_v2(database,[query UTF8String], -1, &statement, nil)== SQLITE_OK) {
				while (sqlite3_step(statement) ==SQLITE_ROW) {
					//int row =sqlite3_column_int(statment, 0);
					NSUInteger rid =sqlite3_column_int(statement, 0);
					NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
					NSString *addr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
					NSString *tel = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
					NSString *label = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,4)];
					
					Restaurant *restaurant =[[Restaurant alloc]init];
					restaurant.rid=rid;
					restaurant.name =name;
					restaurant.addr =addr;
					restaurant.tel =tel;
					restaurant.label=label;
					[restaurantList addObject:restaurant];
					
					[restaurant release];
					//NSLog(@"id --%d",row);
					
				}
				sqlite3_finalize(statement);
				
		}
		
	}
	sqlite3_close(database);
	return restaurantList;
}

+(Restaurant*)getRestaurantById:(NSUInteger )rid{
	sqlite3 *database;
	
	Restaurant *restaurant =[[Restaurant alloc] init];
	
	if (sqlite3_open([[FileHelper dataFilePath] UTF8String], &database)==SQLITE_OK) {
		NSString *query =@"SELECT ID, NAME,ADDR,TEL,LABEL, REGION,RUN_TIME,BUS,BUSI_REGION,DESC from FOODINFO  where ";
		query =[query stringByAppendingFormat:@" ID = %d",rid];
		
		NSLog(@"query --%@",query);
		sqlite3_stmt *statement;
		if (sqlite3_prepare_v2(database,[query UTF8String], -1, &statement, nil)== SQLITE_OK) {
			while (sqlite3_step(statement) ==SQLITE_ROW) {
				//int row =sqlite3_column_int(statment, 0);
				NSUInteger rid =sqlite3_column_int(statement, 0);
				NSString *name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
				NSString *addr = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
				NSString *tel = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
				NSString *label = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,4)];
				NSString *region = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 5)];
				NSString *runTime = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 6)];
				NSString *bus = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 7)];
				NSString *area= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,8)];
				NSString *desc= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,9)];
				
				
				restaurant.rid=rid;
				restaurant.name =name;
				restaurant.addr =addr;
				restaurant.tel =tel;
				restaurant.label=label;
				restaurant.region=region;
				restaurant.runTime=runTime;
				restaurant.bus=bus;
				restaurant.area=area;
				restaurant.desc=desc;
				//[restaurantList addObject:restaurant];
				
				//[restaurant release];
				
				
			}
			sqlite3_finalize(statement);
			
		}
		
	}
	sqlite3_close(database);
	return restaurant;
	
}

@end
